package site.shihuan.helpdesk.im.message.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.Data;
import site.shihuan.helpdesk.im.message.BaseMessage;

/**
 * ContactMessage.
 *
 * @author 周世焕
 * @Date 2021/2/10 10:28
 */
@Data
public class RtcMessage extends BaseMessage {

    public final static String PROPERTY_CMD_TYPE = "C";

    public final static String PROPERTY_PAYLOAD = "P";

    public final static String PROPERTY_LOCAL = "L";

    public final static String PROPERTY_REMOTE = "R";

    private String cmd;

    private String local;

    private String remote;

    private Object payload;

    public RtcMessage() {
        super(TYPE_RTC_CONNECT);
    }

    @Override
    public void decode(String data) {
        JSONObject jsonObject = JSONUtil.parseObj(data);
        cmd = jsonObject.getStr(PROPERTY_CMD_TYPE);
        local = jsonObject.getStr(PROPERTY_LOCAL);
        remote = jsonObject.getStr(PROPERTY_REMOTE);
        payload = jsonObject.get(PROPERTY_PAYLOAD);
    }

    @Override
    public String encode() {
        JSONObject result = new JSONObject();
        result.set(PROPERTY_TYPE,TYPE_RTC_CONNECT);
        JSONObject content = new JSONObject();
        content.set(PROPERTY_CMD_TYPE,cmd);
        content.set(PROPERTY_LOCAL,local);
        content.set(PROPERTY_REMOTE,remote);
        content.set(PROPERTY_PAYLOAD,payload);
        result.set(PROPERTY_CONTENT,content);
        return result.toStringPretty();
    }
}
